# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from datetime import timedelta
from datetime import date, datetime
from dateutil import relativedelta
import json
import time

from openerp.osv import fields, osv
from openerp.tools.float_utils import float_compare, float_round
from openerp.tools.translate import _
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, DEFAULT_SERVER_DATE_FORMAT
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT as DF
from openerp import SUPERUSER_ID, api
from openerp import models, fields as newfields, _
import openerp.addons.decimal_precision as dp
from openerp.addons.procurement import procurement
import logging


_logger = logging.getLogger(__name__)

class stock_move(models.Model):

    _inherit = 'stock.move'

    @api.model
    def get_uom_qty(self,product_id,uos_qty,uos_id=None):
        uom = product_id.uom_id
        uos = uos_id and uos_id or product_id.uos_id
        uos = uos and uos or uom_id
        if uos != uom:
           return uom._compute_qty(uos.id, uos_qty, uom.id)

    @api.model
    def do_move(self,product_id,uos_qty,picking_id,location_dest_id=None):
        product = self.env['product.product'].browse([product_id])
        print(product)
        stock_move_obj = self.env['stock.move']
        domain=[
            ('picking_type_id','=',picking_id),
            ('product_id','=',product_id),
            ('state','in',['assigned','confirmed','waiting'])]
        moves = stock_move_obj.search(domain)
        uom_qty =  self.get_uom_qty(product,uos_qty)
        for move in moves:
            if uom_qty >= move.product_uom_qty:
                uom_qty -= move.product_uom_qty
                if location_dest_id:
                   move.write({
                    'location_dest_id',location_dest_id
                    })
                move.action_done()
            else:
                split_count = uom_qty - move.product_uom_qty
                move.product_uom_qty = uom_qty

                new_move = move.copy()
                new_move.product_uom_qty = split_count

                if location_dest_id:
                   move.write({
                    'location_dest_id',location_dest_id
                    })
                move.action_done()

                new_move.action_confirm()
                new_move.action_done()
                break;


    @api.model
    def get_todo_list(self,picking_type_id=7):
        picking_type = self.env['stock.picking.type'].browse([picking_type_id])
        domain=[('picking_type_id','=',picking_type_id),('state','in',['assigned','confirmed','waiting'])]
        fields = ['product_id','picking_id','picking_type_id','product_uom_qty','location_id','location_dest_id']
        groupby= ['product_id']
        list_todo = self.env['stock.move'].read_group(domain, fields ,groupby,lazy=True,limit=False,offset=0,orderby='date DESC')
        list_todo_view = []
        for item in list_todo:
            product = self.env['product.product'].browse([item['product_id'][0]])
            attributes = []
            for attribute in  product.attribute_value_ids:
                attributes.append({'name':attribute.attribute_id.name,'value':attribute.name})
            uom_id = product.uom_id
            uos_id = product.uos_id
            product_uom_qty = item['product_uom_qty']
            product_uos_qty = product_uom_qty
            if uos_id:
                product_uos_qty = uom_id._compute_qty(uom_id.id, product_uom_qty, uos_id.id)
            uos_id = uos_id and uos_id or uom_id
            list_todo_view.append({
                'product_id':item['product_id'][0],
                'location_id':picking_type.default_location_src_id.id,
                'location_id_name':picking_type.default_location_src_id.name,

                'location_dest_id':picking_type.default_location_dest_id.id,
                'location_dest_id_name':picking_type.default_location_dest_id.name,

                'attributes':attributes,
                'product_uos_qty':product_uos_qty,
                'uos_name':uos_id.name,
                })
        print(list_todo_view)
        return list_todo_view


